package app.server.project.main.service.service;

import app.server.project.core.exception.SysError;
import app.server.project.core.util.AssertUtil;
import app.server.project.main.service.constants.GlobalCfgKey;
import app.server.project.main.service.db.dao.cfg.GlobalCfgDao;
import app.server.project.main.service.db.entity.cfg.GlobalCfgEntity;
import app.server.project.main.service.service.auth.tg.TGAuthService;
import app.server.project.main.service.service.cms.GMService;
import app.server.project.main.service.service.recharge.StarRecharge;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
@Slf4j
public class BotPushService {

    private final TGAuthService tgAuthService;
    private final StarRecharge starRecharge;
    private final GlobalCfgDao globalCfgDao;
    private final GMService gmService;

    private int index;
    private String url;
    private String msg;
    private List<Long> openIds = new ArrayList<>();
    private boolean send = false;


    public void uploadMsg(MultipartFile file) {
        this.url = this.gmService.uploadSingleImg(file);
    }

    public void sendMsg(String msg) {
        AssertUtil.isTrue(StrUtil.isNotBlank(this.url), SysError.EmptyStr);
        AssertUtil.isTrue(StrUtil.isNotBlank(msg), SysError.EmptyStr);
        AssertUtil.isTrue(!send, SysError.Repeat_Again);
        this.msg = msg;
        this.openIds = new ArrayList<>(this.tgAuthService.getTgIdSet());
        this.send = true;
        GlobalCfgEntity globalCfgEntity = new GlobalCfgEntity();
        globalCfgEntity.setId(GlobalCfgKey.BOT_PUSH_Status);
        globalCfgEntity.setVal("发送中");
        globalCfgEntity.setRemark("机器人推送状态");
        globalCfgDao.saveOrUpdate(globalCfgEntity);
        this.globalCfgDao.init();
    }


    @Scheduled(fixedDelay = 1000)
    public void push() {
        if (!send) return;
        if (this.openIds.isEmpty()) return;
        var size = 29;
        List<Long> sendLst = new ArrayList<Long>();
        //分页控制
        if ((index + 1) * size >= openIds.size()) {
            sendLst = this.openIds.subList(index * size, openIds.size());

            for (var openId : sendLst) {
                this.starRecharge.sendMsg(url, msg, openId);
            }
            //发送完毕
            send = false;
            index = 0;
            this.url = null;
            this.msg = null;
            GlobalCfgEntity globalCfgEntity = new GlobalCfgEntity();
            globalCfgEntity.setId(GlobalCfgKey.BOT_PUSH_Status);
            globalCfgEntity.setVal("发送完毕");
            globalCfgEntity.setRemark("机器人推送状态");
            globalCfgDao.saveOrUpdate(globalCfgEntity);
            //log.info("已发送：{}", sendLst);
            this.globalCfgDao.init();
        } else {
            sendLst = this.openIds.subList(index * size, (index + 1) * size);
            for (var openId : sendLst) {
                this.starRecharge.sendMsg(url, msg, openId);
            }
            //log.info("已发送：{}", sendLst);
        }

        index++;
    }
}
